/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Administrator
 * @create 2019/3/13 0013
 */

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2019/3/13 0013
 */

import Service.YibaiChandaoTaskServiceImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;


public class TestMain {
    public static void main(String[] args){
        YibaiChandaoTaskServiceImpl getData = new YibaiChandaoTaskServiceImpl();
        List<Map<String, Object>> list =  getData.getAll("2019-02",2);
        Map<String, Object> list2 = changeDataStructure(list);
        JSONObject json = (JSONObject) JSONObject.toJSON(list2);

        System.out.println(json.toString());
    }

    private static  Map<String,Object> changeDataStructure(List<Map<String, Object>> resultlist) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String,Object>> noList = new ArrayList();
        List<Map<String,Object>> hasList = new ArrayList();
        List<Map<String,Object>> task1List = new ArrayList();
        List<Map<String,Object>> task2List = new ArrayList();
        List<Map<String,Object>> demandList = new ArrayList();
        Set<String> demandSet = new HashSet();
        Set<String> app1Set = new HashSet();
        Set<String> app2Set = new HashSet();

        Map<String,Object> resultMap = new HashMap<>();
        for (Map<String, Object> row: resultlist) {

            System.out.println(JSON.toJSON(row).toString());

            Integer str = (Integer) row.get("story");
            if(str==null){
                str = 0;
            }
            if(str>0){
                // 系统名称
                String xiangmn = (String) row.get("xiangmn");
                // 需求名称
                String xuqmc = (String) row.get("demandNoName");
                // 任务名称
//                String renwmc = (String) row.get("taskNoName");
                // 任务列表
                app1Set.add(xiangmn);
                demandSet.add(xuqmc);
                task1List.add(row);
            }else{
                // 系统名称
                String xiangmn = (String) row.get("xiangmn");
                // 任务名称
//                String renwmc = (String) row.get("taskNoName");
                app2Set.add(xiangmn);
                task2List.add(row);
            }
        }

        List<Map<String,Object>> tempTask = new ArrayList();
        for (String demand : demandSet) {
            Map<String,Object> demandName = new HashMap<>();
            List<Map<String,Object>> tempTaskList = new ArrayList<>();
            String appTemp = null;
            String demandCreatePerson = null;
            String demandCreateDate = null;
            String demandReviewPerson = null;
            String demandReviewDate = null;
            Integer chandaoNum = null;
            String demandTempName = null;
            for (Map<String,Object> task:task1List) {
                String demandTemp = (String) task.get("demandNoName");

                if(!StringUtils.isNullOrEmpty(demandTemp) && demand.equals(demandTemp)){
                    String appTest = (String) task.get("xiangmn");
                    String demandCreatePersonTest = (String) task.get("xuqtcr");
                    Date demandCreateDateTest = (Date) task.get("xuqtcsj");

                    String demandReviewPersonTest = (String) task.get("xuqpsr");
                    Date demandReviewDateTest = (Date) task.get("xuqpssj");
                    String dm = (String) task.get("xuqmc");
                    Integer storyNum = (Integer) task.get("story");
                    if(!StringUtils.isNullOrEmpty(appTest)){
                        appTemp = appTest;
                    }
                    if(!StringUtils.isNullOrEmpty(demandCreatePersonTest)){
                        demandCreatePerson = demandCreatePersonTest;
                    }
                    if(demandCreateDateTest != null){


                        demandCreateDate = sdf.format(demandCreateDateTest);;
                    }
                    if(!StringUtils.isNullOrEmpty(demandReviewPersonTest)){
                        demandReviewPerson = demandReviewPersonTest;
                    }
                    if(demandReviewDateTest != null){
                        demandReviewDate = sdf.format(demandReviewDateTest);
                    }
                    if( storyNum != null ){
                        chandaoNum = storyNum;
                    }
                    demandTempName = (!StringUtils.isNullOrEmpty(dm)) ? dm : demand;
                    tempTaskList.add(task);

                }
            }
            demandName.put("task", tempTaskList);
            demandName.put("xiangmn",demandTempName);
            demandName.put("appName",appTemp);
            demandName.put("demandCreatePerson", demandCreatePerson);
            demandName.put("demandCreateDate", demandCreateDate);
            demandName.put("demandReviewPerson", demandReviewPerson);
            demandName.put("demandReviewDate", demandReviewDate);
            demandName.put("storyNum", chandaoNum);
            demandList.add(demandName);
        }
        for (String app:app1Set) {
            Map appNameTemp = new HashMap();
            List tempDemandList = new ArrayList();
            for(Map<String,Object> map : demandList){
                String appNametemp = (String) map.get("appName");
                if(app.equals(appNametemp)){
                    tempDemandList.add(map);
                }
            }
            appNameTemp.put("appName", app);
            appNameTemp.put("demandList",tempDemandList);
            hasList.add(appNameTemp);
        }

        // 自建任务列表
        for (String app : app2Set) {
            Map<String,Object> appTempMap = new HashMap<>();
            List<Map<String,Object>> tempTaskList = new ArrayList<>();
            String appTemp = null;
            for (Map<String,Object> task:task2List) {
                appTemp = (String) task.get("xiangmn");
                if(!StringUtils.isNullOrEmpty(appTemp) && app.equals(appTemp)){
                    tempTaskList.add(task);
                }
            }
            appTempMap.put("task", tempTaskList);
            appTempMap.put("appName",appTemp);
            noList.add(appTempMap);
        }

        resultMap.put("hasDemand", hasList);
        resultMap.put("noDemand", noList);
        return resultMap;
    }
}